<?php

defined('THINK_PATH') OR exit();
class WidgetsAction extends AliziAction{
	
	public function form($request){
		
		$page = strtolower(MODULE_NAME);
		if($page=='order'){
			$page = isset($_GET['tpl'])?trim($_GET['tpl']):'single';
		}
		$sn = trim($request['id']);
		
		$aliziConfig = parent::aliziConfig();
		$info = getCache('Item',array('sn'=>$sn));
		$shipping = empty($info['shipping_id'])?array('id'=>0):getCache('Shipping',array('id'=>$info['shipping_id']));

		$cookie = array();
		if($aliziConfig['record_order']==1){ $cookie = cookie('order');$cookie['region'] = explode(' ', $cookie['region']);}
		$cookie['ac']=cookie('ac');
		$cookie['uid']=cookie('uid');

		if(in_array($page,array('index','item')) && $request['page']!='detail'){
			$template = array(
				'options'=>$aliziConfig['order_options'],
				'theme'=>$aliziConfig['system_theme'],
				'show_notice'=>$aliziConfig['show_notice'],
			);
		}else{
			$template = getCache('ItemTemplate',array('id'=>$info['id']),true);
			if(empty($template))$template['options'] = $aliziConfig['order_options'];
			$template['extend'] = json_decode($template['extend'],true);
		}
		if(!empty($request['template'])) $template = array_merge($template,$request['template']);

	    $productarrs=json_decode($info['product_set_arrs'],true);
		
		$productattarrs=array();
		$i=0;
		// foreach($productarrs as $product_set_arrs){
			 // // print_r($product_set_arrs);
			  // // print_r('</br>');
		   // $productattarrs[$i]['title']=$product_set_arrs['title'];
		   // $productattarrs[$i]['product_name']=$product_set_arrs['product_name'];
		   // $productattarrs[$i]['product_price']=$product_set_arrs['product_price'];
		   // foreach($product_set_arrs['product_id'] as $product_id){
			  // $product_info  = M('Item')->where(array('id'=>$product_id))->field('id,extends,params_name,params,params_type,image,thumb')->find();
			  // $productattarrs[$i]['product_id'][]=$product_info;
			  // // print_r($productattarrs);break;
			  // // print_r('</br>');
		   // }
		   // $i=$i+1;
		// }
		
		
		
		// var_dump($info['set_arrs']);
		//数组$info['set_arrs']转化为数组$set_arrs
		$set_arrs=array();
		$products=array();
		$i=0;$k=0;
		foreach($productarrs as $val){
			//获取每一个产品的对应数据 product_id  product_name product_price  放入一个数组
			foreach($val['product_id'] as $key=>$product_id){
				//计数加上
				$j=$i+$key;
				$products[$k][$j]['product_id']=$val['product_id'][$key];
				$products[$k][$j]['product_name']=$val['product_name'][$key];
				$products[$k][$j]['product_price']=$val['product_price'][$key];
				$products[$k][$j]['product']=M('Item')->where(array('id'=>$val['product_id'][$key]))->field('id,extends,params_name,params,params_type,image,thumb')->find();
			}
			//获取每一个产品的对应数据 product_id  product_name product_price  放入一个数组
			
			
			//计数加上数组长度
			$i=$i+count($val['product_id']);
			$set_arrs[$k]['title']=$val['title'];
			$k=$k+1;
		}
		// $set_arrs['products']=$products;
		
	  
	  
	  foreach($products as $key1=>$product){
		  
		  $set_arrs[$key1]['products']=array_values($product);
	  }
	  
	  $productattarrs=json_encode($set_arrs,true);
	  //数组$info['set_arrs']转化为数组$set_arrs
	    // print_r(json_encode($set_arrs,true));
		
		// print_r(json_encode($productattarrs,true));
		// print_r($productattarrs);
		
		$list = array(
			'params'=>parent::getItemParams($template['options'],$request['options']),
			'product'=>json_decode($info['params'],true),
			'extends'=>json_decode($info['extends'],true),
			'product_set_arrs'=>$productattarrs,
			'payment'=>parent::getAliziPayment($sn),
			'aliziConfig'=>$aliziConfig,
			'cookie'=>$cookie,
			'color'=>json_decode($template['color'],true),
			'token'=>password($info['id'].get_client_ip()),
		);
		
		
		foreach($list['payment'] as $payment){ $list['paymentDefault'] = $payment;break; }
		extract($list);
	
		$html = include("./Public/Common/alizi.form.{$template['template']}.php");
		if(!$html){
			$tpl = isset($request['formTemplate'])?$request['formTemplate']:'order';
			$html = include('./Public/Common/alizi.form.'.$tpl.'.php');  // 
		}
		return $html;
	}
	
	public function comment($request){
		$sn = trim($request['id']);
		$aliziConfig = parent::aliziConfig();
		$info = getCache('Item',array('sn'=>$sn));
		
		$comments = M('Comments')->where('status=1 AND item_id='.$info['id'])->order('add_time desc')->select();
		if(!empty($comments)){
			return include("./Public/Common/alizi.comment-{$request['commentTpl']}.php");
		}else{
			return include("./Public/Common/alizi.commentemp-1.php");
		}
	}
}	
	


